package com.ruoyi.common.utils;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class DistanceUtil {
    public static void main(String[] args) {
        System.out.println("经纬度距离计算结果：" + getDistance("109.371319", "22.155406", "108.009758", "21.679011") + "米");
    }

    //单位公里
    public static int getDistance(String longitudeFrom, String latitudeFrom, String longitudeTo, String latitudeTo) {
        if(StringUtils.isEmpty(longitudeFrom) || StringUtils.isEmpty(latitudeFrom)){
            return 1;
        }
        GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(latitudeFrom), Double.valueOf(longitudeFrom));
        GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(latitudeTo), Double.valueOf(longitudeTo));

        return ((int)new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target).getEllipsoidalDistance());
    }

    //字符串 保留2位 单位公里
    public static String getJuLiToKM(String longitudeFrom, String latitudeFrom, String longitudeTo, String latitudeTo){
        int juLi = getDistance(longitudeFrom,latitudeFrom,longitudeTo,latitudeTo);
        return BigDecimal.valueOf(juLi).divide(BigDecimal.valueOf(1000)).setScale(2, RoundingMode.HALF_UP)+"KM";
    }

}
